package io.github.cocoa.module.socket.controller.admin.sample;

import com.corundumstudio.socketio.SocketIOServer;
import io.github.cocoa.framework.common.pojo.CommonResult;
import io.github.cocoa.framework.common.pojo.PageResult;
import io.github.cocoa.framework.common.util.collection.SetUtils;
import io.github.cocoa.framework.excel.core.util.ExcelUtils;
import io.github.cocoa.module.socket.controller.admin.sample.vo.SampleInfoCreateReqVO;
import io.github.cocoa.module.socket.controller.admin.sample.vo.SampleInfoExcelVO;
import io.github.cocoa.module.socket.controller.admin.sample.vo.SampleInfoExportReqVO;
import io.github.cocoa.module.socket.controller.admin.sample.vo.SampleInfoImportExcelVO;
import io.github.cocoa.module.socket.controller.admin.sample.vo.SampleInfoImportRespVO;
import io.github.cocoa.module.socket.controller.admin.sample.vo.SampleInfoPageReqVO;
import io.github.cocoa.module.socket.controller.admin.sample.vo.SampleInfoRespVO;
import io.github.cocoa.module.socket.controller.admin.sample.vo.SampleInfoUpdateReqVO;
import io.github.cocoa.module.socket.convert.sample.SampleInfoConvert;
import io.github.cocoa.module.socket.dal.dataobject.sample.SampleInfoDO;
import io.github.cocoa.module.socket.service.sample.SampleInfoService;
import io.github.cocoa.module.system.api.mail.dto.MailSendSingleToUserReqDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static io.github.cocoa.framework.common.pojo.CommonResult.success;

/**
 * @author James
 */
@Tag(name = "示例信息")
@RestController
@RequestMapping("/module-socket/sample/info")
@Validated
public class SampleInfoController {

    @Resource
    private SampleInfoService sampleInfoService;

    @Resource
    private SocketIOServer socketIOServer;

    @PostMapping("/push")
    public CommonResult<Boolean> pushMsgByService(@RequestBody SampleInfoCreateReqVO reqVO) {
        socketIOServer.getBroadcastOperations().sendEvent("all", reqVO.getName());
        return success(true);
    }

    @PostMapping("/create")
    @Operation(summary = "创建示例信息")
    public CommonResult<Long> createInfo(@Valid @RequestBody SampleInfoCreateReqVO createReqVO) {
        return success(sampleInfoService.createInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新示例信息")
    public CommonResult<Boolean> updateInfo(@Valid @RequestBody SampleInfoUpdateReqVO updateReqVO) {
        sampleInfoService.updateInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除示例信息")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteInfo(@RequestParam("id") Long id) {
        sampleInfoService.deleteInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得示例信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1")
    public CommonResult<SampleInfoRespVO> getInfo(@RequestParam("id") Long id) {
        SampleInfoDO info = sampleInfoService.getInfo(id);
        return success(SampleInfoConvert.INSTANCE.convert(info));
    }

    @GetMapping("/list")
    @Operation(summary = "获得示例信息列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "2,3")
    public CommonResult<List<SampleInfoRespVO>> getInfoList(@RequestParam("ids") Collection<Long> ids) {
        List<SampleInfoDO> list = sampleInfoService.getInfoList(ids);
        return success(SampleInfoConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得示例信息分页")
    public CommonResult<PageResult<SampleInfoRespVO>> getInfoPage(@Valid SampleInfoPageReqVO pageVO) {
        PageResult<SampleInfoDO> pageResult = sampleInfoService.getInfoPage(pageVO);
        return success(SampleInfoConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出示例信息 Excel")
    public void exportInfoExcel(@Valid SampleInfoExportReqVO exportReqVO,
                                HttpServletResponse response) throws IOException {
        List<SampleInfoDO> list = sampleInfoService.getInfoList(exportReqVO);
        // 导出 Excel
        List<SampleInfoExcelVO> data = SampleInfoConvert.INSTANCE.convertListExcel(list);
        ExcelUtils.write(response, "export.xls", "Sheet1", SampleInfoExcelVO.class, data);
    }

    @GetMapping("/import-template")
    @Operation(summary = "获得导入示例信息模板")
    public void importInfoTemplate(HttpServletResponse response) throws IOException {
        // 手动创建导出 demo
        List<SampleInfoImportExcelVO> dataList = Collections.singletonList(
                SampleInfoImportExcelVO.builder()
                        .id(1L).name("Sample")
                        .sampleIds(SetUtils.asSet(2L))
                        .build()
        );
        // 输出 Excel 模板
        ExcelUtils.write(response, "import.xls", "Sheet1", SampleInfoImportExcelVO.class, dataList);
    }

    @PostMapping("/import-excel")
    @Operation(summary = "导入示例信息 Excel")
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true),
            @Parameter(name = "updateSupport", description = "是否支持更新，默认为 false", example = "true")
    })
    public CommonResult<SampleInfoImportRespVO> importInfoExcel(
            @RequestParam("file") MultipartFile file,
            @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) throws Exception {
        List<SampleInfoImportExcelVO> list = ExcelUtils.read(file, SampleInfoImportExcelVO.class);
        return success(sampleInfoService.importInfoList(list, updateSupport));
    }

    @GetMapping("/send-mail")
    @Operation(summary = "RPC 发送邮箱消息")
    public CommonResult<Long> sendSingleMailToAdmin() {
        MailSendSingleToUserReqDTO reqDTO = new MailSendSingleToUserReqDTO();
        reqDTO.setMail("username@example.com");
        reqDTO.setTemplateCode("admin-sms-login");
        Map<String, Object> templates = new HashMap<>();
        templates.put("code", "7777");
        templates.put("name", "James");
        reqDTO.setTemplateParams(templates);
        return sampleInfoService.sendSingleMailToAdmin(reqDTO);
    }

    @GetMapping("/send-message")
    @Operation(summary = "发送示例信息消息")
    public CommonResult<Long> sendSampleInfoMessage() {
        return success(sampleInfoService.sendSampleInfoMessage());
    }

    @GetMapping("/send-one")
    @Operation(summary = "发送示例信息消息 - 方法一")
    public CommonResult<Long> oneMethodMessage() {
        return success(sampleInfoService.oneMethodMessage());
    }

    @GetMapping("/send-two")
    @Operation(summary = "发送示例信息消息 - 方法二")
    public CommonResult<Long> twoMethodMessage() {
        return success(sampleInfoService.twoMethodMessage());
    }

}
